package com.talos.ums.service.socialuser;

import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.ums.controller.admin.socialuser.vo.UmsSocialUserPageReqVO;
import com.talos.ums.controller.admin.socialuser.vo.UmsSocialUserSaveReqVO;
import com.talos.ums.dal.dataobject.socialuser.UmsSocialUserDO;
import com.talos.ums.dal.mysql.socialuser.UmsSocialUserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.talos.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_NOT_FOUND;
import static com.talos.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 社交用户表 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class UmsSocialUserServiceImpl implements UmsSocialUserService {

    @Resource
    private UmsSocialUserMapper socialUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createSocialUser(UmsSocialUserSaveReqVO createReqVO) {
        // 插入
        UmsSocialUserDO socialUser = BeanUtils.toBean(createReqVO, UmsSocialUserDO.class);
        socialUserMapper.insert(socialUser);
        // 返回
        return socialUser.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSocialUser(UmsSocialUserSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialUserExists(updateReqVO.getId());
        // 更新
        UmsSocialUserDO updateObj = BeanUtils.toBean(updateReqVO, UmsSocialUserDO.class);
        socialUserMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSocialUser(Long id) {
        // 校验存在
        validateSocialUserExists(id);
        // 删除
        socialUserMapper.deleteById(id);
    }

    private void validateSocialUserExists(Long id) {
        if (socialUserMapper.selectById(id) == null) {
            throw exception(DATA_NOT_FOUND);
        }
    }

    @Override
    public UmsSocialUserDO getSocialUser(Long id) {
        return socialUserMapper.selectById(id);
    }

    @Override
    public PageResult<UmsSocialUserDO> getSocialUserPage(UmsSocialUserPageReqVO pageReqVO) {
        return socialUserMapper.selectPage(pageReqVO);
    }

}
